<?php
namespace Home\Controller;
use Think\Controller;
class FriendController extends CommonController {

    //好友列表
    public function index(){
        $this -> assign('test','好友列表');
            $this -> assign('num',5);//第一次初始化时显示的用户数量
            $this->assign('ajaxurl', U('Friend/ringdel'));
            $this->assign('yesstr', '删除好友成功!');
            $this->assign('nostr', '删除好友失败!');
            $this->display();
    }

    //好友列表瀑布流调用的ajax脚本
    public function ajaxfriend(){
        //只有使用ajax访问的时候 才是被允许的
        if(IS_AJAX){
            //得到需要传输的数据的数量
            $num = $_POST['num'];
            $search = $_POST['search'];
            //实例化ring
            $ring = M('ring');
            $res = $ring
            -> field('zz_user.username,zz_user.user_id,zz_user_info.sex,zz_user_info.photo,zz_user_info.nickname')
            -> where("user_id_a IN (select user_id_b from zz_ring where user_id_a = ".$_SESSION['home']['uid'].") AND zz_user_info.nickname LIKE '%".$search."%' AND user_id_b =".$_SESSION['home']['uid'])
            -> join('zz_user ON zz_user.user_id = zz_ring.user_id_a')
            -> join('zz_user_info  ON zz_user_info.user_id = zz_ring.user_id_a')
            ->page($num, 5)
            ->select();
            // echo $ring -> _sql();
            // var_dump($res);
            //将查询结果转换成json格式
            $this->ajaxReturn($res);
        }else{
            $this -> error('非法操作');
        }
    }

    //好友请求列表
    public function request(){
            $this -> assign('test','好友请求');
            $this -> assign('num',5);//第一次初始化时显示的用户数量
            $this->assign('ajaxurl', U('Friend/ringadd'));
            $this->assign('yesstr', '添加好友成功!');
            $this->assign('nostr', '添加好友失败!');
            $this->display();
    }

    //好友请求用ajax 脚本
    public function ajaxrequest(){
        //只有使用ajax访问的时候 才是被允许的
        if(IS_AJAX){
            //得到需要传输的数据的数量
            $num = $_POST['num'];
            $search = $_POST['search'];
            //得到当前用户的ID
            $id = $_SESSION['home']['uid'];
            //得到相互是好友的ID
            $ring = M('ring');
            // $res = $ring
            // -> field('user_id_a')
            // -> where("user_id_a IN (select user_id_b from zz_ring where user_id_a = ".$id.")  AND user_id_b =".$id)
            // ->select();
            // echo $ring -> _sql();
            // var_dump($res);
            //得到单方的关系ID
            // $ress = $ring
            // -> field('user_id_a')
            // -> where("user_id_b=".$id)
            // ->select();
            // echo $ring -> _sql();
            // var_dump($ress);
            $arr = $ring
            -> field('zz_user.user_id,zz_user.username,zz_user_info.sex,zz_user_info.nickname,zz_user_info.photo')
            ->where('zz_ring.user_id_a IN (SELECT `user_id_a` FROM `zz_ring` WHERE ( user_id_b='.$id.' )) AND zz_ring.user_id_a NOT IN (SELECT `user_id_a` FROM `zz_ring` WHERE ( user_id_a IN (select user_id_b from zz_ring where user_id_a = '.$id.') AND user_id_b ='.$id.' )) ')
            -> join('zz_user ON zz_user.user_id = zz_ring.user_id_a')
            -> join('zz_user_info ON zz_user_info.user_id = zz_ring.user_id_a')
            -> group('zz_user.user_id')
            ->page($num, 5)
            -> select();
            // echo $ring -> _sql();
            $this -> ajaxReturn($arr );
        }else{
            $this -> error('非法操作');
        }
    }


    //好友删除调用的ajax脚本
    public function ringdel(){
        //这里请使用ajax访问
        //这里请使用ajax访问
        if (IS_AJAX) {
            //实例化ring表
            $ring = M('ring');
            $data['user_id_a'] = $_SESSION['home']['uid'];
            $data['user_id_b'] = $_POST['pd'];
            $datas['user_id_b'] = $_SESSION['home']['uid'];
            $datas['user_id_a'] = $_POST['pd'];
            // $ring -> create($data);
            $res = $ring -> where($data) -> delete();
            $ress = $ring -> where($datas) -> delete();
            // echo $ring -> _sql();
            if ($res && $ress) {
                echo 'yes';
            }else{
                echo 'no';
            }
        }else{
            $this -> error('非法操作');
        }
    }

    //好友广场 瀑布流形式显示 不是好友的用户
    public function ring(){
            $this -> assign('test','好友广场');
            $this -> assign('num',5);//第一次初始化时显示的用户数量
            $this->assign('ajaxurl', U('Friend/ringadd'));
            $this->assign('yesstr', '添加好友成功!');
            $this->assign('nostr', '添加好友失败!');
            $this->display();
    }

    //好友广场瀑布流调用的ajax脚本
    public function ajaxring(){
        //只有使用ajax访问的时候 才是被允许的
        if(IS_AJAX){
        // var_dump($_POST);die;
            //得到需要传输的数据的数量
            $num = $_POST['num'];
            $search = $_POST['search'];
            //得到用户的ID
            $data['user_id_a'] = $_SESSION['home']['uid'];
            //扫描好友关系表
            $ring = M('ring');
            //得到好友的ID
            $res = $ring -> field('user_id_b') ->where($data) ->select();
            // echo $ring -> _sql();die;
            if (!$res) {
                $res = array();
            }
            $noid = array_column($res, 'user_id_b');
            array_push( $noid , $_SESSION['home']['uid']);
            $noid = implode(',', $noid);
            // echo $noid;
            // die;
            //得到非好友的用户的详细信息
            $user = M('user');
            $info = $user
            ->field('zz_user.user_id,zz_user.username,zz_user_info.sex,zz_user_info.photo,zz_user_info.nickname')
            ->where("zz_user.user_id NOT IN (".$noid.") AND zz_user_info.nickname LIKE '%".$search."%'")
            ->join('zz_user_info ON zz_user.user_id  = zz_user_info.user_id')
            ->page($num,5)
            ->order('zz_user.user_id')
            -> select();
            // echo $user->_sql();
            //将查询结果转换成json格式
            $this->ajaxReturn($info);
        }else{
            $this -> error('非法操作');
        }
    }

    //好友广场添加好友调用的ajax脚本
    public function ringadd(){
        //这里请使用ajax访问
        if (IS_AJAX) {
            //实例化ring表
            $ring = M('ring');
            $data['user_id_a'] = $_SESSION['home']['uid'];
            $data['user_id_b'] = $_POST['pd'];
            $ring -> create($data);
            $res = $ring -> add();
            if ($res) {
                echo 'yes';
            }else{
                echo 'no';
            }
        }else{
            $this -> error('非法操作');
        }

    }


    //好友请求数
    public function getNewFriend(){
        //只有使用ajax访问的时候 才是被允许的
        if(IS_AJAX){
            //得到当前用户的ID
            $id = $_SESSION['home']['uid'];
            //得到相互是好友的ID
            $ring = M('ring');
            // $res = $ring
            // -> field('user_id_a')
            // -> where("user_id_a IN (select user_id_b from zz_ring where user_id_a = ".$id.")  AND user_id_b =".$id)
            // ->select();
            // echo $ring -> _sql();
            // var_dump($res);
            //得到单方的关系ID
            // $ress = $ring
            // -> field('user_id_a')
            // -> where("user_id_b=".$id)
            // ->select();
            // echo $ring -> _sql();
            // var_dump($ress);
            $res = $ring
            -> field('zz_user.user_id,zz_user.username,zz_user_info.sex,zz_user_info.nickname,zz_user_info.photo')
            ->where('zz_ring.user_id_a IN (SELECT `user_id_a` FROM `zz_ring` WHERE ( user_id_b='.$id.' )) AND zz_ring.user_id_a NOT IN (SELECT `user_id_a` FROM `zz_ring` WHERE ( user_id_a IN (select user_id_b from zz_ring where user_id_a = '.$id.') AND user_id_b ='.$id.' )) ')
            -> join('zz_user ON zz_user.user_id = zz_ring.user_id_a')
            -> join('zz_user_info ON zz_user_info.user_id = zz_ring.user_id_a')
            -> group('zz_user.user_id')
            -> select();
            // echo $ring -> _sql();
            $count=count($res);
            $this -> ajaxReturn($count);
        }else{
            $this -> error('非法操作');
        }
    }

}
